package com.df.springmvc.system.staff.services;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.bean.DhtmlGridBean;
import com.common.bean.DhtmlGridFieldBean;
import com.common.util.SN_GUID;
import com.common.util.DhtmlGrid.DhtmlGridAttribuate;
import com.common.util.security.EncrypMD5;
import com.df.springmvc.supper.SupperServices;
import com.df.springmvc.system.log.services.LogServices;
import com.df.springmvc.system.staff.dao.StaffDao;

/**
 * 本类只做数据运算处理
 * @author David
 */
@Service
public class StaffServices extends SupperServices{
	@Autowired
	StaffDao dao;
	@Autowired
	LogServices logServices;
	
	public List<Object> findAllEmployees(){
		// TODO Auto-generated method stub
		return null; 
	}
	public DhtmlGridBean query(DhtmlGridBean gridBean,HashMap<String,String> param_HashMap) throws Exception{
		// TODO Auto-generated method stub
		//CurrentUserBean  userBean=CurrentUserBean.loadFromSession();
		Object[] attribute=new Object[]{};
		gridBean.cleanSessionXmlFile();
		DhtmlGridAttribuate attribuate=new DhtmlGridAttribuate();
		attribuate.addFieldBean(new DhtmlGridFieldBean("","",DhtmlGridFieldBean.TYPE_CH,"40"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("guid","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("staffCode","<center></center>",DhtmlGridFieldBean.TYPE_ED,"0"));
		attribuate.addFieldBean(new DhtmlGridFieldBean("staffNumCode","员工工号",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("staffName","员工姓名",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("staffName","出生年月",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("orgCode","所属部门",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("position","职务",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("telphone","手机号码",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("telphone","手机号码2",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("email","电子邮箱",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		attribuate.addFieldBean(new DhtmlGridFieldBean("incumbencyStatus","在职状态",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_CENTER));
		attribuate.addFieldBean(new DhtmlGridFieldBean("userAccount","用户账号",DhtmlGridFieldBean.TYPE_ED,"*",DhtmlGridFieldBean.ALIGN_LEFT));
		/**
		 * 增加对应的业务代码
		 */
		ArrayList<Object> list=null;
		gridBean.setAttribuate(attribuate);
		list=dao.query(param_HashMap,attribute,0,2000);
		for(int i=0;i < list.size();i++){
			gridBean.addData(list.get(i));
			if((i + 1) % gridBean.getPageSize() == 0){
				gridBean.createSessionXmlFile();
			}
		}
		gridBean.createSessionXmlFile();
		return gridBean;
	}
	@Override
	public Object add(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		String message= (String) dao.queryVerify(paramMap);
		if(message!= null){
			return message;
		}else{
			HashMap<String,Object> map=new HashMap<String,Object>(paramMap);
			map.put("guid",SN_GUID.getUUID());
			map.put("status","1");
			map.put("createby",getCurrentUserBean().getUserId());
			map.put("createTime",new Date());
			map.put("staffCode",SN_GUID.getUUID_Num());
			map.put("incumbencyStatus","1");
			Object o = dao.add(map);
			if("0".equals(o)){
				logServices.add("用户日志","员工管理","add","添加员工信息失败");
				return "添加失败！";
			}else{
				logServices.add("用户日志","员工管理","add","添加员工信息成功");
				return "添加成功！";
			}
		}
	}
	/**
	 * 为员工创建账号
	 * @param paramMap
	 * @return
	 * @throws Exception
	 */
	public Object createAccount(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		String message= (String) dao.queryVerifyAccount(paramMap);
		if(message!= null){
			return message;
		}else{
			HashMap<String,Object> map=new HashMap<String,Object>(paramMap);
			map.put("guid",SN_GUID.getUUID());
			map.put("status","1");
			map.put("accountStatus","1");
			map.put("createby",getCurrentUserBean().getUserId());
			map.put("createTime",new Date());
			map.put("userType","1");
			map.put("password", EncrypMD5.encode("123456"));
			Object o = dao.createAccount(map);
			if("0".equals(o)){
				logServices.add("用户日志","员工管理","createAccount","为员工创建失败");
				return "创建失败！";
			}else{
				logServices.add("用户日志","员工管理","createAccount","为员工创建成功");
				return "创建成功！";
			}
		}
	}
	@Override
	public Object upd(HashMap<String,String> map) throws Exception{
		// 编号是否重复
		String message=(String)dao.queryVerifyUpd(map);
		if(message != null){
			return message;
		}
		HashMap<String,Object> updmap=new HashMap<String,Object>(map);
		updmap.put("updateby",getCurrentUserBean().getUserId());
		updmap.put("updatetime",new Date());
		Object o=dao.upd(updmap);
		if("1".equals(o)){
			logServices.add("用户日志","员工管理","upd","修改员工信息成功");
			return "修改成功！";
		}else{
			logServices.add("用户日志","员工管理","upd","修改员工信息失败");
			throw new Exception("修改失败！");
		}
	}
	/**
	 * 员工离职
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public Object leave(HashMap<String,String> map) throws Exception{
		HashMap<String,Object> updmap=new HashMap<String,Object>(map);
		updmap.put("updateby",getCurrentUserBean().getUserId());
		updmap.put("updatetime",new Date());
		Object o=dao.leave(updmap);
		if("1".equals(o)){
			logServices.add("用户日志","员工管理","leave","修改员工离职状态信息成功");
			return "修改成功！";
		}else{
			logServices.add("用户日志","员工管理","leave","修改员工离职状态信息失败");
			throw new Exception("修改失败！");
		}
	}
	@Override
	public Object del(HashMap<String,String> paramMap) throws Exception{
		// TODO Auto-generated method stub
		int successCount=0;
		int errorCount=0;
		for(String item:("" + paramMap.get("guid")).split(",")){
			HashMap<String,Object> map=new HashMap<String,Object>();
			map.put("guid",item);
			map.put("status", 0);
			if("1".equals(dao.del(map))){
				successCount++;
			}else{
				errorCount++;
			}
		}
		logServices.add("用户日志","员工管理","del","删除员工信息成功[" + successCount + "]失败[" + errorCount + "]");
		return "成功[" + successCount + "]失败[" + errorCount + "]";
	}
	@Override
	public HashMap<String,Object> view(String guid) throws Exception{
		// TODO Auto-generated method stub
		return dao.view(guid);
	}
	/**
	 *获取参与人员弹出层展示
	 * @param loginName  当前登录人员
	 */ 
	public Object getStaffAll(String loginName) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<HashMap<String,Object>> list=new ArrayList<HashMap<String,Object>>();
		list=dao.getStaffAll(loginName);
		return list;
	}

}
